package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysCombo;
import com.ruoyi.system.domain.SysDishes;
import com.ruoyi.system.domain.SysEndCombo;
import com.ruoyi.system.mapper.SysMainComboMapper;
import com.ruoyi.system.mapper.SysOnlyComboMapper;
import com.ruoyi.system.mapper.SysTotalDishesMapper;
import com.ruoyi.system.service.ISyMainComboService;
import com.ruoyi.system.service.ISyOnlyComboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMainComboServiceImpl implements ISyMainComboService {
    @Autowired
    private SysMainComboMapper sysMainComboMapper;
    @Autowired
    private SysTotalDishesMapper sysTotalDishesMapper;
    @Override
    public List<SysEndCombo> selectOnlyComDisList(SysEndCombo sysEndComboo) {
        List<SysEndCombo> sysEndCombos = new ArrayList<>();
        List<SysCombo> sysCombos = sysMainComboMapper.selectOnlyComDisList(sysEndComboo);
        sysCombos.stream().forEach(combo->{
            SysEndCombo sysEndCombo = new SysEndCombo();
            sysEndCombo.setComboName(combo.getComboName());
            sysEndCombo.setComboTypeName(combo.getComboTypeName());
            sysEndCombo.setComboDishesPrice(combo.getComboDishesPrice());
            sysEndCombo.setComboDescription(combo.getComboDescription());
            sysEndCombo.setComboStatus(combo.getComboStatus());
            sysEndCombo.setPackageId(combo.getPackageId());
            sysEndCombo.setComboDishesId(combo.getComboDishesId());
            sysEndCombo.setComboImage(combo.getComboImage());
            sysEndCombo.setComboId(combo.getComboId());
            String comboCode = combo.getComboCode();
            List<SysDishes> sysDishes = new ArrayList<>();
            for (String dishesIdAndQuantity : comboCode.split("\\+")) {

                String[] split = dishesIdAndQuantity.split("\\*");
                String dishesId = split[0];
                System.out.println(dishesId);
                SysDishes dishes = sysTotalDishesMapper.selectSysDishesByDishesId(Long.valueOf(dishesId));
                String dishesQuantity = split[1];
                dishes.setDishesQuantity(dishesQuantity);
                sysDishes.add(dishes);
            }
            sysEndCombo.setDishesList(sysDishes);

            sysEndCombos.add(sysEndCombo);

        });
        return sysEndCombos;
    }

    @Override
    public int updateSysComboById(SysCombo sysCombo) {

        return sysMainComboMapper.updateSysComboById(sysCombo);

    }

    /*根据combo_dishes_id查看套餐信息*/
    @Override
    public SysEndCombo selectSysComboByComboDishesId(Long comboDishesId) {
        SysCombo sysCombo = sysMainComboMapper.selectSysComboByComboDishesId(comboDishesId);
        System.out.println(sysCombo);
        System.out.println(comboDishesId);
        SysEndCombo sysEndCombo = new SysEndCombo();
        sysEndCombo.setComboName(sysCombo.getComboName());
        sysEndCombo.setComboDescription(sysCombo.getComboDescription());
        sysEndCombo.setPackageId(sysCombo.getPackageId());
        sysEndCombo.setComboDishesPrice(sysCombo.getComboDishesPrice());
        sysEndCombo.setComboStatus(sysCombo.getComboStatus());
        sysEndCombo.setComboTypeName(sysCombo.getComboTypeName());
        sysEndCombo.setComboCode(sysCombo.getComboCode());
        sysEndCombo.setComboDishesId(sysCombo.getComboDishesId());
        sysEndCombo.setComboImage(sysCombo.getComboImage());
        sysEndCombo.setComboId(sysCombo.getComboId());
        List<SysDishes> sysDishes = new ArrayList<>();
        for (String dishesIdAndQuantity : sysCombo.getComboCode().split("\\+")) {
            String[] split = dishesIdAndQuantity.split("\\*");
            String dishesId = split[0];
            System.out.println(dishesId);
            SysDishes dishes = sysTotalDishesMapper.selectSysDishesByDishesId(Long.valueOf(dishesId));
            String dishesQuantity = split[1];
            dishes.setDishesQuantity(dishesQuantity);
            sysDishes.add(dishes);
        }
        sysEndCombo.setDishesList(sysDishes);

        return sysEndCombo;
    }

    @Override
    public int insertCombo(SysCombo sysCombo) {
        return sysMainComboMapper.insertCombo(sysCombo);
    }

    @Override
    public int deleteByComboDishesId(Long[] comboDishesIds) {
        return sysMainComboMapper.deleteByComboDishesId(comboDishesIds);
    }
}
